package com.yinxin.reflection;

import java.lang.reflect.Method;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.List;
import java.util.Map;

//获取泛型的类型
public class TestGetGeneric {
    public void test01(Map<String,User> map, List<User> list){
        System.out.println("test01");
    }
    public Map<String,User> test02(){
        System.out.println("test02");
        return null;
    }
    public static void main(String[] args) throws NoSuchMethodException {
        Method method = TestGetGeneric.class.getMethod("test01", Map.class, List.class);//获取方法
        Type[] genericParameterTypes = method.getGenericParameterTypes();//获取方法参数的类型数组
        for (Type genericParameterType : genericParameterTypes) {//遍历它
            System.out.println(genericParameterType+"=================参数");
            if(genericParameterType instanceof ParameterizedType){
                Type[] actualTypeArguments = ((ParameterizedType) genericParameterType).getActualTypeArguments();
                for (Type actualTypeArgument : actualTypeArguments) {
                    System.out.println(actualTypeArgument+"===============泛型类型");
                }
            }
        }
        System.out.println("$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$$");
        Method test02 = TestGetGeneric.class.getMethod("test02", null);//Map.class
        Type genericReturnType = test02.getGenericReturnType();
        if(genericReturnType instanceof ParameterizedType){
            System.out.println(genericReturnType+"========================参数");
            Type[] actualTypeArguments = ((ParameterizedType) genericReturnType).getActualTypeArguments();//实际参数类型
            for (Type actualTypeArgument : actualTypeArguments) {
                System.out.println(actualTypeArgument+"=======================泛型类型");
            }
        }
    }
}
